﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class Reservations
    {
        public static int CreateReservation(int tourId, int rtypeId, decimal tPrice)
        {
            if (tPrice >= 0)
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from t in context.Tours
                     where t.TourId == tourId
                     select t).FirstOrDefault();

                if(res != null)
                {
                    Reservation b = new Reservation()
                    {
                        TourId = tourId,
                        ReservationTypeId = rtypeId,
                        TotalPrice = tPrice,
                        RefNum = res.TourId + res.StartDate.ToString()
                    };

                    context.Reservations.InsertOnSubmit(b);
                    context.SubmitChanges();

                    return b.ReservationId;
                }
            }
            return -1;
        }

        public static void UpdateReservation(int resId, int tourId, int rtypeId, decimal tPrice)
        {
            if (tPrice >= 0)
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from b in context.Reservations
                     where b.ReservationId == resId
                     select b).FirstOrDefault();

                var res2 =
                    (from t in context.Tours
                     where t.TourId == tourId
                     select t).FirstOrDefault();

                if (res != null && res2 != null)
                {
                    res.TourId = tourId;
                    res.TotalPrice = tPrice;
                    res.ReservationTypeId = rtypeId;
                    res.RefNum = res2.TourId + res2.StartDate.ToString();
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteReservation(int resId)
        {

            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                (from b in context.Reservations
                 where b.ReservationId == resId
                 select b).FirstOrDefault();

            if (res != null)
            {
                context.Reservations.DeleteOnSubmit(res);
                context.SubmitChanges();
            }
        }

        public static Reservation GetReservationById(int resId)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();
            var res =
                (from r in context.Reservations
                 where r.ReservationId == resId
                 select r).FirstOrDefault();

            return res;
        }

        public static IQueryable<Reservation> GetReservations()
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            return context.Reservations;
        }

        public static void AddToPrice(int resId, decimal pr)
        {
            Reservation r = GetReservationById(resId);
            if (r != null)
            {
                decimal newPrice = r.TotalPrice + pr;
                UpdateReservation(r.ReservationId, r.TourId, r.ReservationTypeId, newPrice);
            }
        }

        public static int NumberOfPeopleOnReservation(int resId)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var ppl =
                (from x in context.ReservationsPersons
                 where x.ReservationId == resId
                 select x).Count();

            return ppl;
        }
    }
}
